import axios from "axios";
const url = 'http://localhost:4000';
// const url = 'http://121.43.104.131:4000';

// 创建axios的实例
const http = axios.create({
    baseURL: url,
    timeout: 20000,
    // headers: { 
    //     token: localStorage.getItem("TOKEN")
    // }
})
// 封装GET请求
const get = (url: string, data = {}) => {
    return http.get(
        `${url}`,
        {
            params: data,
            headers: { 
                token: localStorage.getItem("TOKEN")
            }
        }
    )
    .then(response => response.data)
}
// 封装POST请求
const post = (url:string, data={}) => {
    return http.post(
        `${url}`, 
         data,
         {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                token: localStorage.getItem("TOKEN")
            }
        }
    )
    .then(response => response.data)
}
// 封装提交图片
const upload = (url:string, data={}) => {
    return http.post(
        `${url}`, 
         data,
         {
            headers: {
                'Content-Type': 'multipart/form-data',
                 token: localStorage.getItem("TOKEN")
            }
        }
    )
    .then(response => response.data)
}
// 导出属性和方法
export default {
    url: 'http://localhost:4000',
    // url: 'http://121.43.104.131:4000',
    get,
    post, 
    upload
}